import ajax from '../../js/ajax.js';
import tools from '../../js/tools.js';

let ae = document.getElementById('ae');

let cs1 = {
  cname: ''
};
let cs2 = {
  pageNumber: 1,
  pageSize: 5
};
let nr = [];

//#region ================班级信息获取==================
function bjhq() {
  let cs = tools.concatJson(cs1, cs2);
  ajax.send('/manage/class/queryAll', cs, (data) => {
    if (data.success) {
      nr = data.list;
      console.log('班级内容', nr);
      cs2 = data.page;
      bjzs();
      fy();
      return;
    }
    alert(data.message);
  });
}

function bjzs() {
  ae.innerHTML = '';
  for (let i = 0; i < nr.length; i++) {
    const et = nr[i];
    let tr = document.createElement('tr');
    let td;
    // 班级编号
    td = document.createElement('td');
    td.append(et.cid);
    tr.append(td);
    // 班级名称
    td = document.createElement('td');
    td.append(et.cname);
    tr.append(td);
    // 班级描述
    td = document.createElement('td');
    td.append(et.cinfo);
    tr.append(td);

    // 信息最后修改时间
    td = document.createElement('td');
    td.append(tools.FormData(et.lastupdate));
    tr.append(td);

    //修改
    td = document.createElement('td');
    let an = document.createElement('button');
    an.append('修改');
    an.addEventListener('click', () => {
      xgff(et);
    });

    //删除
    td = document.createElement('td');
    let an2 = document.createElement('button');
    an2.append('删除');
    an2.addEventListener('click', () => {
      scff(et);
    });

    
    td.append(an);
    td.append(an2);
    tr.append(td);

    ae.append(tr);
  }
}
//#endregion

//#region ================分页部分=================
let fenye1 = document.querySelectorAll('nav>a');
let fenye2 = document.querySelector('nav>span');

function fy() {
  fenye2.innerHTML = `
  记录数/当前页/总页数
  ${cs2.total}/${cs2.pageNumber}/${cs2.pageCount}
  `;
}

fenye1[0].addEventListener('click', () => {
  if (cs2.pageNumber == 1) {
    return;
  }
  cs2.pageNumber = 1;
  bjhq();
});

fenye1[1].addEventListener('click', () => {
  cs2.pageNumber--;
  if (cs2.pageNumber < 1) {
    cs2.pageNumber = 1;
    return;
  }
  bjhq();
});

fenye1[2].addEventListener('click', () => {
  cs2.pageNumber++;
  if (cs2.pageNumber > cs2.pageCount) {
    cs2.pageNumber = cs2.pageCount;
    return;
  }
  bjhq();
});

fenye1[3].addEventListener('click', () => {
  if (cs2.pageNumber == cs2.pageCount) {
    return;
  }
  cs2.pageNumber = cs2.pageCount;
  bjhq();
});
//#endregion

//#region ================查询======================
let bjcx = document.getElementById('bjcx');
let cx = document.getElementById('cx');
let cz = document.getElementById('cz');
cx.addEventListener('click', () => {
  cs1.cname = bjcx.value;
  cs2.pageNumber = 1;
  bjhq();
});
cz.addEventListener('click', () => {
  bjcx.value = '';
  cx.click();
});
//#endregion

//#region ================增加=======================

let zj = document.getElementById('zj');
let tczs = document.getElementById('tczs');
let bjmc = document.getElementById('bjmc');
let bjjs = document.getElementById('bjjs');
let tj = document.getElementById('tj');

zj.addEventListener('click', () => {
  tczs.showModal();
});
tj.addEventListener('click', () => {
  let csnr = {
    cname: bjmc.value,
    cinfo: bjjs.value
  };
  ajax.send('/manage/class/add', csnr, (data) => {
    if (data.success) {
      bjmc.value = '';
      bjjs.value = '';
    }
    alert(data.message);
  });
});

//#endregion

//#region ================修改=======================
let xg = document.getElementById('xg');
let tczs2 = document.getElementById('tczs2');
let bjmc2 = document.getElementById('bjmc2');
let bjjs2 = document.getElementById('bjjs2');

let id;
function xgff(csnr2) {
  bjmc2.value = csnr2.cname;
  bjjs2.value = csnr2.cinfo;
  id = csnr2.cid;
  tczs2.showModal();
}
xg.addEventListener('click', () => {
  let csnr2 = {
    cid: id,
    cname: bjmc2.value,
    cinfo: bjjs2.value
  };
  ajax.send('/manage/class/update', csnr2, (data) => {
    alert(data.message);
  });
});

tczs2.addEventListener('close', () => {
  cs2.pageNumber = 1;
  bjhq();
});
//#endregion

//#region ================删除=======================
//dqdj:当前点击所传入的参数
function scff(dqdj) {
  if (confirm(`是否删除：${dqdj.cname}`)) {
    ajax.send('/manage/class/delete', { cid: dqdj.cid }, (data) => {
      alert(data.message);
      cs2.pageNumber = 1;
      bjhq();
    });
  }
}
//#endregion

bjhq();
